package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.dbutils.handlers.ScalarHandler;
import com.nf.xiaomi.dao.UserDao;
import com.nf.xiaomi.entity.UsersEntity;
import com.nf.xiaomi.util.SendSmsUtils;
import com.nf.xiaomi.util.SqlExecutorUtils;

import java.util.List;

public class UserDaoImpl implements UserDao {
    SqlExecutor executor = SqlExecutorUtils.getExecutor();
    public Integer insert(UsersEntity users) {
        String sql = "insert into user_info(user_account,user_password,user_name,user_gender,user_phone,user_picture,user_location,address_id,nstate) " +
                "values(?,?,?,?,?,?,?,?,?)";
        return executor.update(sql, users.getUserAccount(),users.getUserPassword(),users.getUserName(),users.getUserGender(),users.getUserPhone(),users.getUserPicture(),users.getUserLocation(),1,users.getState());    }

    public Integer update(UsersEntity users) {
        String sql = "update user_info set user_account = ?,user_password = ?,user_name = ?,user_gender = ?,user_phone= ?," +
                "user_picture=?,user_location=?,address_id = ?,nstate= ? where user_id = ?";
        return executor.update(sql, users.getUserAccount(),users.getUserPassword(),users.getUserName(),users.getUserGender(),users.getUserPhone(),users.getUserPicture(),users.getUserLocation(),users.getAddressID(),users.getState(),users.getUserID());
    }

    public Integer deleteByID(Long account) {
        String sql="delete from user_info where user_account=?";
        return executor.update(sql, account);
    }

    public Long login(Long account, String password) {
        String sql = "select count(*) from user_info where user_account = ? or user_phone = ? and user_password=?";
        return executor.query(sql, new ScalarHandler<Long>(), account,account, password);
    }

    public Long register(Long phone) {
        String sql = "select count(*) as num from user_info where user_phone=? ";
        return executor.query(sql, new ScalarHandler<>(),phone);
    }

    public List<UsersEntity> getAll() {
        String sql = "select u.user_id,u.user_account,u.user_password,a.user_name,u.user_gender,a.user_phone,u.user_picture,u.user_date,u.user_location,a.address_content from user_info u\n" +
                "left join address a on u.address_id = a.address_id";
        return executor.query(sql, new BeanListHandler<>(UsersEntity.class));
    }

    public List<UsersEntity> getAll(Long account) {
        String sql = "select u.user_id,u.user_account,u.user_password,a.user_name,u.user_gender,a.user_phone,u.user_picture,u.user_date,u.user_location,a.address_content from user_info u\n" +
                "left join address a on u.address_id = a.address_id where u.user_account = ? or u.user_phone = ?";
        return executor.query(sql, new BeanListHandler<>(UsersEntity.class), account,account);
    }

    public List<UsersEntity> getAll(String name) {
        String sql = "select u.user_id,u.user_account,u.user_password,a.user_name,u.user_gender,a.user_phone,u.user_picture,u.user_date,u.user_location,a.address_content from user_info u\n" +
                "left join address a on u.address_id = a.address_id where u.user_account like ? or u.user_name like ?";
        return executor.query(sql, new BeanListHandler<>(UsersEntity.class), "%"+name+"%","%"+name+"%");
    }

    public Integer getAllID(Long account) {
        String sql = "select user_id from user_info where user_account=?";
        return executor.query(sql,new ScalarHandler<Integer>(),account);
    }

    public String sendSms(Long phone) {
        String result = "123456";
//        String result = SendSmsUtils.SendSms(phone);
        return result;
    }
}
